package com.xros.demo.gamedataserver.handler;

import com.xros.demo.gamedataserver.codec.CustomerMessageCodec;
import com.xros.demo.gamedataserver.decoder.CustomFrameDecoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.springframework.stereotype.Component;

/**
 * @Author: mzp
 * @Description: work通道初始化handler
 * @Date: 2024/3/26 14:23
 * @Version: 1.0
 */
@Component
public class WorkerInitChannelHandler extends ChannelInitializer<SocketChannel> {


    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        //编码、解码器
        CustomerMessageCodec messageCodec = new CustomerMessageCodec();

        //日志处理器
        LoggingHandler loggingHandler = new LoggingHandler(LogLevel.INFO);


        //消息封装处理器
        MsgWrapperHandler msgWrapperHandler = new MsgWrapperHandler();

        //前置输入处理器
        PrefixInboundHandler prefixInboundHandler = new PrefixInboundHandler();

        socketChannel.pipeline()
                //添加日志处理器
                .addLast(loggingHandler)
                .addLast(prefixInboundHandler)
                //添加帧解码器
                .addLast(new CustomFrameDecoder())
                //消息解码、编码器
                .addLast(messageCodec)
                //消息处理器
                .addLast(msgWrapperHandler);


    }
}
